var HomeScene = (function(){

    function HomeScene(){
        Scene2D.call(this,true);
        this.musicPlay = true;
        this.gameRoom = null;
        this.gameNeedRset = false;
    }

    HomeScene.prototype = new Scene2D();
    var __proto = HomeScene.prototype;

    __proto.initScene = function(node){
        this.bg.height += offH;
        this.bg.y -= offH/2;
        //this._node.y += offH/2;
        this.btnStartGame.on(Laya.Event.MOUSE_DOWN, this, this.startGame);
        GameSceneObj.resetGame();
        //排行
        this.btnRank.on(Laya.Event.MOUSE_DOWN, HomeSceneObj, HomeSceneObj.goRank);
        //分享
        this.btnShare.on(Laya.Event.MOUSE_DOWN, HomeSceneObj, HomeSceneObj.goShare);
        //声音设置
        this.btnMusic.on(Laya.Event.MOUSE_DOWN, HomeSceneObj, HomeSceneObj.goSet);

        BaseData.initSceneGameData(this);

    };

    __proto.onActive = function(from){
        //声音设置
        if(Config.channelID == 1){
            //广告视频
            this.title.skin = "common/title.png"
        }else{
            this.title.skin = "common/title_ytdd.png"
            this.btnMusic.visible = false;
            this.btnRank.visible = false;
            this.btnShare.visible = false;
        }
        
        if(Config.isAd){
            AdUtil.showBanner(true);
        }

        if(Config.channelID == 101) {
            // // oppo原生广告
            // Laya.timer.once(1000, this, this.onNativeIcon);
            // this.banner_icon_ani.play();
        }
    };

    __proto.onInActive = function(){
    };
    
    //开始游戏
    __proto.startGame = function(){
        Tool.log("start_game");
        if(this.gameNeedRset){
            GameSceneObj.resetGame();
        }
        Config.start_game_time = (new Date()).getTime()
        if(Config.channelID == 2 && typeof BK != "undefined"){
            SceneManager.toggleAction("start_game");
            // this.gameRoom = new BK.Room();
            // // 手Q环境，正式上线时使用 NETWORK_ENVIRONMENT_QQ_RELEASE 或 0 或不设置
            // // 手Q环境，调试时使用 NETWORK_ENVIRONMENT_QQ_DEBUG  或 1
            // // 开发环境,使用demo工程开发时使用 NETWORK_ENVIRONMENT_DEMO_DEV 或2
            // this.gameRoom.environment = NETWORK_ENVIRONMENT_QQ_DEBUG;
            // var self = this;
            // // BK.Director.ticker.add(function(ts,duration){
            // //     self.gameRoom.updateSocket();
            // // });
            // this.gameRoom.createAndJoinRoom(GameStatusInfo.gameId,GameStatusInfo.openId,function (statusCode,room) {
            //     Tool.log("加入房间回调-----statusCode："+statusCode);
            //     if(statusCode == 0){
            //         Tool.log("当前玩家：");
            //         room.currentPlayers.forEach(function(player) {
            //             Tool.log("recvJoinRoom opeid:"+player["openId"] ); //openid
            //             Tool.log("recvJoinRoom joinTs:"+player["joinTs"] );//加入的时间戳
            //             Tool.log("recvJoinRoom status:"+player["status"] );//玩家状态 1用户在游戏中 2 用户加入后离开 3 用户等待加入
            //         }, self);
            //         Config.start_game_time = (new Date()).getTime()
            //         self.gameRoom.startGame(function (statusCode) {
            //             SceneManager.toggleAction("start_game");
            //         });
            //     }    
            // }); 
        }else{
            SceneManager.toggleAction("start_game");
        }

        
    };

    //声音设置
    __proto.goSet = function(){
        this.musicPlay = !this.musicPlay;
        if(this.musicPlay){
            Audio.resumeAll();
            this.btnMusic.skin = "common/voice.png";
        }else{
            Audio.pauseAll();
            this.btnMusic.skin = "common/voice1.png";
        }        
    };

    //排行
    __proto.goRank = function(){
        var __RankDlg = null;
        if(Config.channelID == 1){
            __RankDlg = new RankDlg();
            __RankDlg.showDlg();
        }
        if(Config.channelID == 2 ){
            __RankDlg = new RankQQDlg();
            __RankDlg.showDlgAndMask();
        }
        
    };

    //分享
    __proto.goShare = function(){
        if(Config.channelID == 1){
            WxApp.onShare();
        }
        if(Config.channelID == 2 && typeof BK != "undefined"){
            shareTitle = titles[Math.floor(Math.random()*titles.length)]
            var shareInfo = {
                summary:shareTitle,          //QQ聊天消息标题
                picUrl:"https://oss.leyungame.com/qqttmwx/"+Config.version+"/wxshare/1.jpg",               //QQ聊天消息图片
                // localPicPath:"GameRes://qqtt_ewm.png",   //分享至空间、微信、朋友圈时需要的图。（选填，若无该字段，系统使用游戏对应的二维码）
                gameName:"球球弹弹"          //游戏名，暂用与生成二维码
            };
            BK.QQ.share(shareInfo, function (retCode, shareDest, isFirstShare) {});
         }
    };  

    /**
     * oppo原生Icon广告
     */
    __proto.onNativeIcon = function() {
        var that = this;
        if(Config.channelID == 101 && Config.isLoadSuccess) {
            // 获取ICON原生广告
            AdUtil.showNativeBanner("nativeIcon", "44891", function(res) {
                // 原生广告获取成功
                Tool.log("-------init oppo ad native ad success-------------------");
                for(var i = 0;i < res.adList.length;i++) {       
                    // 广告标识
                    Tool.log("-------init oppo ad native ad success adId -------", res.adList[i].adId);
                    // 广告标题
                    Tool.log("-------init oppo ad native ad success title -------", res.adList[i].title);
                    // 广告描述
                    Tool.log("-------init oppo ad native ad success desc -------", res.adList[i].desc);
                    // 推广应用的Icon图标
                    Tool.log("-------init oppo ad native ad success iconUrlList -------", res.adList[i].iconUrlList);
                    // 广告图片
                    Tool.log("-------init oppo ad native ad success imgUrlList -------", res.adList[i].imgUrlList);
                    // “广告”标签图片
                    Tool.log("-------init oppo ad native ad success logoUrl -------", res.adList[i].logoUrl);
                    // 点击按钮文本描述
                    Tool.log("-------init oppo ad native ad success clickBtnTxt -------", res.adList[i].clickBtnTxt);
                    // 获取广告类型，取值说明：0：无 1：纯文字 2：图片 3：图文混合 4：视频
                    Tool.log("-------init oppo ad native ad success creativeType -------", res.adList[i].creativeType);
                    // 获取广告点击之后的交互类型，取值说明： 0：无 1：浏览类 2：下载类 3：浏览器（下载中间页广告） 4：打开应用首页 5：打开应用详情页
                    Tool.log("-------init oppo ad native ad success interactionType -------", res.adList[i].interactionType);
                }
                // 目前只有一个
                // 保存原生ICON广告的ID
                Config.adIconId = res.adList[0].adId;
                // 加载原生ICON广告         
                that.native_banner_icon.skin = res.adList[0].iconUrlList[0];
                that.native_banner_text.text = res.adList[0].title;
                that.native_banner_icon.on(Laya.Event.MOUSE_DOWN, that, that.onNativeIconClick);
            }, function(err) {
                Tool.log("-------init oppo ad native ad onError-------------------", err.code);
                Tool.log("-------init oppo ad native ad onError-------------------", err.msg);
            });

            return;
        }
        Laya.timer.once(1000, this, this.onNativeIcon);
    }

    /**
     * 原生ICON广告曝光
     */
    __proto.onNativeIconClick = function() {
        if(Config.channelID == 101) {
            Tool.log("-------init oppo ad native ad 原生ICON广告点击");
            // oppo原生ICON广告曝光,切换oppo原生Icon广告
            this.onNativeIcon();
            Config.nativeIcon.reportAdShow({
                adId: Config.adIconId 
            })
            Config.nativeIcon.reportAdClick({
                adId: Config.adIconId
            })
        }else if(Config.channelID == 102) {
            Config.vivoNative.onClick();
        }
    }

    return HomeScene;
})();